//
//  PayViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/8.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "PayViewModel.h"

@implementation PayViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    _pay4Type = 0;
    _chooseType = 0;
    
    _payCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        @weakify(self);
        return [[self _requestCalWXPaySignal:self.orderSN] flattenMap:^RACStream *(id value)
        {
            @strongify(self);
            
            return [self _requestPayNotifySignal];
        }];
    }];
}

- (RACSignal*)_requestPayNotifySignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                NSString* url = self.pay4Type==0 ? url_interface_order_paynotify : url_interface_spread_paynotify;
                NSDictionary* postData = self.pay4Type==0 ? interface_param_order_paynotify(self.orderID) : interface_param_spread_paynotify(self.orderID, self.orderSN, um.userData.userID);
                
                [[AFNetWorkUtils racPOSTWthURL:url params:postData] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:@"PayFinish"];
                     [subscriber sendCompleted];
                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"支付回调失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

- (RACSignal*)_requestCalWXPaySignal:(NSString*)tradeNo
{
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        
        NSString* price = self.money;
        
        NSDecimalNumber *priceDN = [NSDecimalNumber decimalNumberWithString:price];
        priceDN = [priceDN decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:@"100"]];
        
        WTPayOrderItem* payOrderItem = [[WTPayOrderItem alloc] init];
        payOrderItem.orderBody = @"";
        payOrderItem.orderName = @"购买生态币订单";
        payOrderItem.orderPrice = [NSString stringWithFormat:@"%d", [priceDN intValue]];
        payOrderItem.orderOutTradeNO = tradeNo;
        
        [WTPayManager wtPayOrderItem:payOrderItem payType:WTPayTypeWeixin result:^(NSDictionary *payResult, NSString *error) {
            
            if (error)
            {
                NSString * errorInfo = error;
                NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
                userInfo[customErrorInfoKey] = errorInfo;
                NSError * error = [NSErrorHelper createErrorWithUserInfo:userInfo domain:@"WTPayManager"];
                [subscriber sendError:error];
                [subscriber sendCompleted];
            }
            else
            {
                [subscriber sendNext:@YES];
                [subscriber sendCompleted];
            }
        }];
        
        return nil;
    }];
}

@end
